<template>
  <div class="menudetail">
    <div class="top">
      <video controls width="100%" height="350" :src="detail.vfurl" v-if="detail.vfurl"></video>
      <img class="auto-img" :src="detail.photo_path" v-else />
      <van-nav-bar left-arrow @click-left="back">
        <template #right>
          <van-icon name="share-o" size="18" />
        </template>
      </van-nav-bar>
    </div>
    <div class="body">
      <div class="title">
        <h2>{{ detail.title }}</h2>
      </div>
      <div class="youlan">
        <span>游览{{ detail.views_count_text }}</span>
        <span>收藏{{ detail.favo_counts_text }}</span>
        <span>学做{{ detail.dish_count }}</span>
      </div>
      <div class="author">
        <div class="authormess">
          <div class="headImg" v-if="detail && detail.user" @click="toUserDetail(detail.user.user_id)">
            <img class="auto-img" v-if="detail && detail.user" :src="detail.user.user_photo" />
          </div>
          <span v-if="detail && detail.user">{{ detail.user.nick }}</span>
          <span style="background-color:gold; padding:5px;margin-left: 5px; border-radius: 5px;"
            v-if="detail && detail.user">LV{{ detail.user.lvl }}</span>
        </div>
        <van-button type="primary" size="small" color="gold" @click="changeisAttention">{{ isAttention ? "取消关注" : "关注"
        }}</van-button>
      </div>
      <p class="sign">{{ detail.cookstory }}</p>
      <h3>食材</h3>
      <ul class="foodMaterial">
        <li v-for="(item, index) in detail.major" :key="index">
          <span>{{ item.title }}</span>
          <span class="dash"></span>
          <span>{{ item.note }}</span>
        </li>
      </ul>
      <div class="nutritionAnalysis">
        <div class="zong">{{ detail.energy }}千卡</div>
        <ul class="nutritionComposition">
          <li v-for="(item, index) in detail.nutrition_facts" :key="index">
            {{ item.nutrient }}<b style="color: #087d76">{{ item.amount }}</b>克
          </li>
        </ul>
      </div>
      <div class="cookStep">
        <h3>烹饪步骤</h3>
        <div class="step" v-for="(item, index) in detail.cookstep" :key="index">
          <p>
            步骤{{ index + 1 }}：<span>{{ item.content }}</span>
          </p>
          <div class="stepsImg">
            <img class="auto-img" :src="item.image" />
          </div>
        </div>
      </div>
      <p class="time">{{ detail.release_time }}</p>
    </div>
    <div
      style="background-color: white; position: fixed; bottom: 0; width: 100%; height: 60px; box-shadow: 0 0 5px; display: flex; align-items: center; font-size: 14px; font-weight: 700;">
      <div>
        <van-cell closeable >
          <input
            style="margin-left: 0; margin-right: 10px; border: 0; background-color: rgb(245,245,245); width: 130px; height: 30px; border-radius: 50px; outline: none; padding: 0 0 0 10px; box-sizing: border-box;"
            type="text" placeholder="说点什么…" />
        </van-cell>
      </div>
      <van-popup v-model:show="show" round position="bottom" :style="{ height: '70%', width: '100%' }">

        <div class="All" style="padding: 10px; font-size: 20px;">全部评论({{ reviewList.cc }})</div>
        <div class="content">
          <div class="authormess" v-for="(item1, index1) in reviewList.comments" :key="index1">
            <div class="authoritem">
              <div class="headImg">
                <img class="auto-img" :src="item1.u.p">
              </div>
              <span>{{ item1.u.n }}</span>
              <span style="color: #707070;">LV{{ item1.u.lvl }}</span>
            </div>
            <div style="background-color: #FFFFFF;
            display: block;">
              <p style="margin-left: 35px;font-size: 16px;">{{ item1.content[0].c }}</p>
              <div class="authormess" v-for="(item2, index2) in item1.child_comments" :key="index2">
                <div class="authoritem" style="background-color: #F6F6F6;">
                  <div style="margin-left: 35px;  font-size: 12px;">{{ item2.u.n }}</div>
                  <div style="color: #707070; font-size: 12px;">:{{ item2.content[0].c }}</div>
                </div>
              </div>
            </div>
          </div>

        </div>

      </van-popup>
      <div style="display: flex; margin-right: 10px;" @click="showPopup"><span>查看评论</span></div>
      <div style="display: flex;margin-right: 10px;">
        <van-icon v-if="store.collect.indexOf(Number(detail.cook_id)) == -1" name="star-o" size="20px" color="#000" @click="getCollection(Number(detail.cook_id))" />
        <van-icon v-else name="star" size="20px" color="#ffd700" @click="store.removeCollectList(Number(detail.cook_id)
        )" />
        <span class="collect">收藏</span>
      </div>
      <div style="margin-right: 10px; display: flex;">
        <img src="../assets/xuezuo.png">
        <span>传学做</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getMenuDetailToId, getrecipeReview } from "../api/api";
import { useHomeStore } from "@/stores/home";
import { useLogin, useLogining } from "../stores/index";
import { useRouter } from "vue-router"


export default {
  name: "menuDetail",
  data() {
    return {
      detail: {},
      authorId: "",
      isAttention: false,
      // menunameId: "2501602",
      active: 0,
      show: false,
      reviewList: {},
      store: ""
      // uselogin,
      // uselogining
    };
  },
  created() {
    // let menunameId = '2501602';
    let menunameId = this.$route.params.menuId;
    //通过axios拿取菜谱详情数据
    this.store = useHomeStore()
    getMenuDetailToId(menunameId)
      .then((res) => {
        this.detail = res.data.result.recipe;
        this.authorId = this.detail.user.user_id;
        let attentionList =
          JSON.parse(localStorage.getItem("attentionList")) || [];
        if (attentionList.includes(Number(this.authorId))) {
          // 如果已关注，则将 isAttention 设置为 true
          this.isAttention = true;
        }
      })
      .catch((err) => {
        console.log("err=>", err);
      });
    // 通过axios拿取菜谱评论数据
    getrecipeReview(menunameId).then((res) => {
      // console.log('这是菜谱评论==>', res);
      this.reviewList = res.data.result
      console.log(res.data.result);

    })
      .catch((err) => {
        console.log("err=>", err);
      });
  },

  methods: {
    back() {
      this.$router.push(this.$route.params.path);
    },
    //评论
    showPopup() {
      this.show = true;
    },
    // 收藏
    getCollection(id) {
      this.store.addCollectList(Number(id))
    },
    //关注或取消关注

    //改变关注状态
    //  changeisAttention(){
    //   if (uselogining.isLogining == 1) {
    //     // 关注
    //     if (!isAttention.value) {
    //       store.addFollowList(Number(userid));
    //     } else {
    //       // 取消关注
    //       store.removeFollowList(Number(userid));
    //     }
    //     isAttention.value = !isAttention.value;
    //   } else {
    //     // 跳转到登录界面
    //     uselogin.updateLogin(true);
    //   }
    // };
    changeisAttention() {
      let attentionList =
        JSON.parse(localStorage.getItem("attentionList")) || [];
      // console.log(attentionList);

      if (this.isAttention) {
        // 如果已关注，则切换到取消关注状态
        this.isAttention = false;
        attentionList = attentionList.filter((id) => id != this.authorId); // 从关注列表中移除用户ID
      } else {
        // 如果未关注，则切换到关注状态
        this.isAttention = true;
        attentionList.push(Number(this.authorId)); // 将用户ID添加到关注列表
      }


      // 将更新后的关注列表保存到 LocalStorage
      localStorage.setItem("attentionList", JSON.stringify(attentionList));
    },
    //添加跳转到用户详情的路由
    toUserDetail(userId) {
      // console.log(userId);
      this.$router.push({ name: 'userdetails', params: { id: userId, path: this.$route.fullPath } })
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .van-nav-bar .van-icon {
  color: #333;
  font-size: 16px;
}

.auto-img {
  width: 100%;
  height: 100%;
}

$color1: #087d76;
$color2: #707070;

.top {
  width: 100%;
  height: 350px;
  position: relative;

  .van-nav-bar {
    position: absolute;
    background-color: transparent;
    z-index: 999;
    top: 0;

  }

  .van-hairline--bottom::after {
    border: none;
  }

  .van-nav-bar .van-icon {
    color: #707070;
  }
}

.body {
  position: relative;
  padding: 0 10px;

  .title {
    background-color: #fff;
    position: absolute;
    top: -30px;
    left: 0;
    right: 0;
    border-radius: 22px;

    h2 {
      padding-left: 10px;
    }
  }

  .youlan {
    padding-top: 25px;
    font-size: 16px;
    color: #087d76;
    margin-bottom: 10px;

    span {
      padding: 0 10px;
      position: relative;

      &::after {
        content: "";
        position: absolute;
        width: 5px;
        height: 5px;
        background-color: #087d76;
        border-radius: 50%;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
      }

      &:first-child {
        padding-left: 0;
      }

      &:last-child::after {
        background-color: #fff;
      }
    }
  }
}

.content {
  .authormess {


    .authoritem {
      display: flex;
      align-items: center;
      margin-bottom: 10px;

      .headImg {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: #000;
        overflow: hidden;
        margin-right: 5px;
        margin-left: 5px;

      }

      span {

        margin-left: 5px;
      }
    }
  }
}

.author {
  display: flex;
  justify-content: space-between;

  .authormess {
    display: flex;
    align-items: center;

    .headImg {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: #000;
      overflow: hidden;
      margin-right: 5px;
    }

    span {
      color: #707070;
    }
  }
}

.sign {
  font-size: 16px;
  text-indent: 2em;
}

.foodMaterial {
  li {
    font-size: 14px;
    display: flex;
    color: #666;
    line-height: 42px;
    align-items: center;

    .dash {
      height: 1px;
      flex: auto;
      border-bottom: 1px dashed #ccc;
      margin: 0 10px;
    }
  }
}

.nutritionAnalysis {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .zong {
    background-color: #087d76;
    color: #fff;
    display: inline-block;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    line-height: 50px;
    text-align: center;
  }

  .nutritionComposition {
    display: flex;
    align-items: center;

    li {
      width: 10px;
      padding: 0 20px;
      position: relative;

      &::before {
        content: "";
        width: 1px;
        height: 40px;
        background-color: #087d76;
        position: absolute;
        right: 0;
        top: 20px;
      }

      &:last-child {
        &::before {
          content: "";
          width: 1px;
          height: 40px;
          background-color: #fff;
          position: absolute;
          right: 0;
          top: 20px;
        }
      }
    }
  }
}

.cookStep {
  .step {
    font-size: 14px;
  }
}

.time {
  color: #707070;

}

.time:last-child {
  margin-bottom: 60px;
}
.collect{
  line-height: 26px;
}
</style>